<?php
/**
 * Model for a project build.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Model for a project build.
 *
 * @package PhpCilux
 */
class Model_Build extends Model_Base
{

    /**
     * The project key of this project.
     *
     * @var string
     */
    protected $_projectKey;

    /**
     * The build key.
     *
     * @var string
     */
    protected $_buildKey;

    /**
     * The log classes of this build.
     *
     * @var array
     */
    protected $_logs = array();

    /**
     * The commit log of this build.
     *
     * @var Model_Log_Svn
     */
    protected $_commitLog = null;

    /**
     * Constructor.
     *
     * @param array $options Several options (key -> value).
     */
    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }

        $this->_logs[] = new Model_Log_PhpCs($this->_projectKey, $this->_buildKey);
        $this->_logs[] = new Model_Log_PhpUnit($this->_projectKey, $this->_buildKey);
        $this->_logs[] = new Model_Log_PhpCpd($this->_projectKey, $this->_buildKey);
        $this->_logs[] = new Model_Log_PhpMd($this->_projectKey, $this->_buildKey);

        $commitLog = new Model_Log_Svn($this->_projectKey, $this->_buildKey);

        if ($commitLog->exists()) {
            $this->_commitLog = $commitLog;
        }
    }

    /**
     * Set the project key.
     *
     * @param string $text The project key.
     *
     * @return Model_Build
     */
    public function setProjectKey($text)
    {
        $this->_projectKey = (string) $text;
        return $this;
    }

    /**
     * Get the project key.
     *
     * @return string
     */
    public function getProjectKey()
    {
        return $this->_projectKey;
    }

    /**
     * Set the build key.
     *
     * @param string $text The build key.
     *
     * @return Model_Build
     */
    public function setBuildKey($text)
    {
        $this->_buildKey = (string) $text;
        return $this;
    }

    /**
     * Get the build key.
     *
     * @return string
     */
    public function getBuildKey()
    {
        return $this->_buildKey;
    }

    /**
     * Get the log classes.
     *
     * @return array The logs
     */
    public function getLogs()
    {
        return $this->_logs;
    }

    /**
     * Get the comment class.
     *
     * @return Model_Log_Svn
     */
    public function getComment()
    {
        return $this->_commitLog;
    }

    /**
     * Get the build status.
     *
     * @return string
     */
    public function getStatus()
    {
        $status = 'success';

        foreach ($this->getLogs() as $log) {
            if ($log->exists()) {
                if ($log->status() == 'failed') {
                    return 'failed';
                } elseif ($log->status() == 'warning') {
                    $status = 'warning';
                }
            }
        }

        return $status;
    }

    /**
     * Get the build name.
     *
     * @return string
     */
    public function getName()
    {
        return $this->getBuildKey();
    }

}